
"use strict";
angular.module('app.project').controller('Add_newProjectCtrl', function ($scope, personalCenterHttp, projectHttpS, jurisHttpS, $http, $rootScope, $stateParams, $cookieStore, HttpInterceptor) {

    
    $scope.infor = {}; // 项目基本信息model
    var pid = $cookieStore.get("edit_pid");
    $rootScope.pname = $cookieStore.get("edit_pname");

    getProjectInfo();
    function getProjectInfo(){
        if(pid){
            //项目基本信息
            $http.get(baseurls+"/detail/project?id=="+pid,{withCredentials:true}).then(function(res){
                console.log(res.data.info);
                if(res.data.ret == 1){
                    var the_pro = res.data.info;

                    $scope.infor.name = the_pro.name;
                    $scope.infor.num = the_pro.num;
                    $scope.infor.clientname = the_pro.clientname;
                    $scope.infor.type = the_pro.type;
                    // $("#project_type option:eq("+the_pro.type+")").prop("selected","selected"); //x
                    $scope.infor.fid = the_pro.fid;
                    $scope.infor.des = the_pro.des;
                    $scope.infor.oiltype = JSON.parse(the_pro.detail).oiltype;
                    $scope.infor.capacity = JSON.parse(the_pro.detail).capacity;
                    $scope.infor.height = JSON.parse(the_pro.detail).height;
                    // $scope.infor.oiltype = the_pro.detail.oiltype;
                    // $scope.infor.capacity = the_pro.detail.capacity;
                    // $scope.infor.height = the_pro.detail.height;
                }
            })
            //项目详细信息
            // $http.get(baseurls+"/detail/project_detail?id=="+pid).then(function(res){
            //     if(res.data.ret == 1){
            //         var the_pro_detail = res.data.info;
            //         $scope.infor.oiltype = the_pro_detail.oiltype;
            //         $scope.infor.volume = the_pro_detail.volume;
            //         $scope.infor.weight = the_pro_detail.weight;
            //         $scope.infor.height = the_pro_detail.height;
            //     }
            // })
        }else{
            $scope.infor = {name:"", num:null, clientname:"", type:"", fid:null, des:"", oiltype:"", capacity:0, height: 0};
        }
        console.log($scope.infor)
    } 
    

    $scope.save_infor = function () { // 点击保存// 判断是创建新项目还是编辑项目
        if(!pid){// 进入的是创建新项目

            $scope.new_project = {
                name: $scope.infor.name,
                num: $scope.infor.num,
                clientname: $scope.infor.clientname,
                type: $('#project_type option:selected').val(),
                fid: $cookieStore.get("user"),
                des: $scope.infor.des,
                detail:{
                    oiltype: $scope.infor.oiltype,
                    capacity: $scope.infor.capacity,
                    height: $scope.infor.height,
                    thermometer:[h1a20,h2a20,h3a21,h4a22,h5a23,h6a24,h7a25,h8a26,h9a27,h10a28,h11a29,h12a30]
                    // [t1:20,t2:20,t3:21,t4:22,t5:23,t6:24,t7:25,t8:26,t9:27,t10:28,t11:29,t12:30]
                }
            };
            // $scope.new_project_detail = {
            //     oiltype: $scope.infor.oiltype,
            //     volume: $scope.infor.volume,
            //     weight: $scope.infor.weight,
            //     height: $scope.infor.height
            // };

            if(!$scope.new_project.name){
                alert("项目名称不能为空")
            }else{
                //保存新项目
                $http.post(baseurls+"/data/project",$scope.new_project).then(function(res){
                    // console.log(res.data);
                    // console.log($scope.new_project);
                    if(res.data.ret == 1){
                        //添加项目详情
                        // $scope.new_project_detail.id = res.data.info;
                        // console.log($scope.new_project_detail);
                        // $http.post(baseurls+"/data/project_detail",$scope.new_project_detail).then(function(res2){
                        //     if(res2.data.ret == 1){
                                new Notify('success','新项目创建成功');
                                window.location.href = appConfig.base[0].baseURI + "#/myallprojects/";//返回所有项目
                        //     }
                        // })
                    }else{
                        new Notify('fail','新项目创建失败');
                    }
                })

            }

        }else{ // 进入的是编辑项目


            $scope.edit_pro_id = {
                id: pid,
                name: $scope.infor.name,
                num: $scope.infor.num,
                clientname: $scope.infor.clientname,
                // type: $('#project_type option:selected').val(),
                type: $scope.infor.type,
                fid: $scope.infor.fid,
                des: $scope.infor.des,
                detail:JSON.stringify({
                    oiltype: $scope.infor.oiltype,
                    capacity: $scope.infor.capacity,
                    height: $scope.infor.height,
                    thermometer:['h1a20','h2a20','h3a21','h4a22','h5a23','h6a24','h7a25','h8a26','h9a27','h10a28','h11a29','h12a30']
                })
            };
            // {"oiltype":"大豆油","capacity":1000,"height":12,"thermometer":["h1a20","h2a20","h3a21","h4a22","h5a23","h6a24","h7a25","h8a26","h9a27","h10a28","h11a29","h12a30"]}
            // $scope.edit_pro_detail = {
            //     oiltype: $scope.infor.oiltype,
            //     volume: $scope.infor.volume,
            //     weight: $scope.infor.weight,
            //     height: $scope.infor.height
            // };

            //保存编辑的项目
            $http.put(baseurls+"/data/project?id=="+pid,$scope.edit_pro_id).then(function(res){
                console.log(res.data);
                console.log($scope.edit_pro_id);
                if(res.data.ret == 1){
                    // $http.put(baseurls+"/data/project_detail?id=="+pid,$scope.edit_pro_detail).then(function(res2){
                    //     if(res2.data.ret == 1){
                            new Notify('success','项目更改保存成功');
                            window.location.href = appConfig.base[0].baseURI + "#/myallprojects/";//返回所有项目
                    //     }
                    // })
                    
                }else{
                    new Notify('fail','项目更改保存失败');
                }
            })
        }
    };

    //  删除该项目
    $scope.del_project = function(){  
        $("#del_project").modal('show');
        $scope.del = function(){
            $http.delete(baseurls+"/data/project?id=="+pid).then(function(res){
                console.log(res.data);
                if(res.data.ret == 1){
                    // $http.delete(baseurls+"/data/project_detail?id=="+pid).then(function(res){
                        $("#del_project").modal('hide');
                        new Notify('success','项目删除成功');
                        window.location.href = appConfig.base[0].baseURI + "#/myallprojects/";//返回所有项目
                    // })
                }else{
                    new Notify('fail','项目删除失败');
                }
            })
        }
    }

});